package com.bt.lms.network.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.constant.TeacherBizTypeConstant;
import com.bt.lms.entity.TeacherBasicInfo;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.service.TeacherBasicInfoService;
import com.bt.lms.service.impl.TeacherBasicInfoServiceImpl;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 新增教师处理器
 * @author 李舒淇
 */
public class TeacherModuleHandler implements ModuleHandler{
    @Override
    public void handlerMsg(ReqMsg reqMsg, Socket socket) {

        //1、获取类型
        Short bizType = reqMsg.getBizType();
        //2、处理数据
        String data = reqMsg.getData();
        //3、调业务接口
        Object obj = bizAdd(bizType,data);
        //4、是否响应
        Byte twoWay = reqMsg.getTwoWay();
        if (twoWay.equals(ReqMsg.NETWORK_TWO_WAY_CHAT)){
            RespMsg respMsg = objReq(reqMsg, obj);
            byte[] msgByte = respMsg.toBytes();
            try {
                OutputStream out = socket.getOutputStream();
                out.write(msgByte);
                out.flush();
                out.close();
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }

    //响应
    private RespMsg objReq(ReqMsg reqMsg, Object obj) {

        String magicNo = reqMsg.getMagicNo();//魔数
        Byte version = reqMsg.getVersion();//协议版本号
        Byte serialCou = reqMsg.getSerialCou();//序列化算法
        Byte evenType = reqMsg.getEvenType();//事件类型
        Short bizType = reqMsg.getBizType();//业务类型
        Long reqId = reqMsg.getReqId();//请求id
        byte[] remainField = reqMsg.getRemainField();//保留字段
        //数据
        String objStr = JSON.toJSONString(obj);

        //响应消息
        RespMsg respMsg = new RespMsg();
        respMsg.setMagicNo(magicNo);
        respMsg.setVersion(version);
        respMsg.setSerialCou(serialCou);
        respMsg.setEvenType(evenType);
        respMsg.setBizType(bizType);
        respMsg.setReqId(reqId);
        respMsg.setRemainField(remainField);
        respMsg.setData(objStr);
        respMsg.setStatus(RespMsg.NETWORK_STATUS_NORMAL);

        return respMsg;
    }

    /**
     * 3、调用业务接口
     * @param bizType
     * @param data
     * @return
     */
    private Object bizAdd(Short bizType, String data) {

        Object obj = null;
        //教师管理接口
        TeacherBasicInfoService teacherInfo = new TeacherBasicInfoServiceImpl();

        if (bizType.equals(TeacherBizTypeConstant.TEACHER_QUERY)){

        }

        if (bizType.equals(TeacherBizTypeConstant.TEACHER_ADD)){
            TeacherBasicInfo teacherBasicInfo = JSONObject.parseObject(data, TeacherBasicInfo.class);
            try {
                obj = teacherInfo.add(teacherBasicInfo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        if (bizType.equals(TeacherBizTypeConstant.TEACHER_DETAILS)){

        }

        if (bizType.equals(TeacherBizTypeConstant.TEACHER_EDIT)){

        }

        if (bizType.equals(TeacherBizTypeConstant.TEACHER_ENABLE)){

        }

        if (bizType.equals(TeacherBizTypeConstant.TEACHER_STOP)){

        }

        return obj;
    }
}
